package com.motionlog.activity;

import java.io.IOException;

import com.motionlog.R;
import com.motionlog.model.Database;

import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class HistoryItem extends Activity implements OnClickListener {

	private static final String LOG_TAG = null;

	public static final String BASE_RECORD_PATH = Environment
			.getExternalStorageDirectory().getAbsolutePath()
			+ "/Android/data/com.motionlog/record";

	Database mDb;

	TextView mMotion;
	TextView mReasons;
	TextView mDescription;
	TextView mMethods;
	TextView mRemark;
	TextView mNowThink;
	TextView mCategories;
	SeekBar mLevel;

	private MediaPlayer mPlayer = null;
	private Button mReasonDescRecord;
	private Button mNowthinkRecord;

	private long mLogId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history_item);

		mDb = Database.getInstance(this);

		mMotion = (TextView) findViewById(R.id.motion);
		mReasons = (TextView) findViewById(R.id.reasons);
		mDescription = (TextView) findViewById(R.id.description);
		mMethods = (TextView) findViewById(R.id.custom_method);
		mRemark = (TextView) findViewById(R.id.remark);
		mNowThink = (TextView) findViewById(R.id.now_think);
		mCategories = (TextView) findViewById(R.id.selected_categories);
		mLevel = (SeekBar) findViewById(R.id.level);
		mLevel.setEnabled(false);

		mReasonDescRecord = (Button) findViewById(R.id.record_reason_desc);
		mReasonDescRecord.setOnClickListener(this);
		mNowthinkRecord = (Button) findViewById(R.id.record_now_think);
		mNowthinkRecord.setOnClickListener(this);

		long logId = getIntent().getLongExtra("logId", -1);
		mLogId = logId;
		Cursor c = mDb.selectLog(logId);
		if (c.moveToNext()) {
			mMotion.setText(c.getString(c.getColumnIndex("name")));
			mReasons.setText(c.getString(c.getColumnIndex("reason_summary")));
			mDescription.setText(c.getString(c
					.getColumnIndex("motion_reason_description")));
			mMethods.setText(c.getString(c.getColumnIndex("method_summary")));
			mRemark.setText(c.getString(c.getColumnIndex("method_markup")));
			mNowThink.setText(c.getString(c.getColumnIndex("now_think")));
			mCategories.setText(c.getString(c
					.getColumnIndex("category_summary")));
			mLevel.setProgress(c.getInt(c.getColumnIndex("level")));
		}
	}

	boolean mIsPlaying = false;

	private void startPlaying(String fileName) {
		mPlayer = new MediaPlayer();
		try {
			mPlayer.setDataSource(fileName);
			mPlayer.prepare();
			mPlayer.start();
		} catch (IOException e) {
			android.util.Log.e(LOG_TAG, "prepare() failed");
		}
		mIsPlaying = true;
	}

	private void stopPlaying() {
		mPlayer.release();
		mPlayer = null;
	}

	@Override
	public void onClick(View v) {
		if (mIsPlaying) {
			stopPlaying();
		}
		switch (v.getId()) {
		case R.id.record_reason_desc:
			String reasonDescPath = BASE_RECORD_PATH + "/log.method.desc."
					+ mLogId + ".3gp";
			startPlaying(reasonDescPath);
			break;
		case R.id.record_now_think:
			String nowthinkPath = BASE_RECORD_PATH + "/log.category.nowthink."
					+ mLogId + ".3gp";
			startPlaying(nowthinkPath);
			break;
		}
	}
}
